package com.mazaiting.easy.base.fragment;

import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
import com.mazaiting.easy.base.presenter.BasePresenter;
import com.mazaiting.easy.config.Constant;
import com.mazaiting.widget.fragment.LoadingDialogFragment;

/**
 * 带有加载进度条的Fragment基类
 */
public abstract class BaseLoadingFragment<T extends BasePresenter> extends BaseLazyFragment<T> {
    /**加载进度条*/
    private LoadingDialogFragment mLoadingDialogFragment;

    @Override
    public void bindView(View view, Bundle savedInstanceState) {

    }

    @Override
    public void onHiddenLoading() {
        // 可用
//        if (mLoadingDialogFragment.isCancelable()) {
//            mLoadingDialogFragment.dismiss();
//        }
        // 可用，如果出现问题，使用上方的代码
        mLoadingDialogFragment.dismissAllowingStateLoss();
    }

    @Override
    public void onShowLoading() {
        onShowLoading("");
    }

    @Override
    public void onShowLoading(String text) {
        if (null == mLoadingDialogFragment) {
            // 创建进度条Fragment
            mLoadingDialogFragment = new LoadingDialogFragment();
        }
        mLoadingDialogFragment.setText(text);
        if (null != getActivity()) {
            // 显示
            mLoadingDialogFragment.show(getActivity().getSupportFragmentManager(), Constant.DIALOG_LOADING);
        }
    }

    @Override
    public void onShowSuccess() {
        this.onHiddenLoading();
    }

    @Override
    public void onShowFailed(String message) {
        this.onHiddenLoading();
    }

    @Override
    public void onShowNoNet() {
        Toast.makeText(getActivity(), "无网络", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onRetry() {

    }
}
